package com.zhiqian.community.controller;

import com.zhiqian.community.entity.Message;
import com.zhiqian.community.entity.Page;
import com.zhiqian.community.entity.User;
import com.zhiqian.community.service.MessageService;
import com.zhiqian.community.service.UserService;
import com.zhiqian.community.util.CommunityUtil;
import com.zhiqian.community.util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/message")
public class MessageController {

    @Autowired
    private MessageService messageService;
    @Autowired
    private HostHolder hostHolder;
    @Autowired
    private UserService userService;

    @GetMapping("/list")
    public String index(Model model, Page page){
        User user = hostHolder.getUser();
        page.setRows(messageService.selectConversationsCount(user.getId()));
        page.setPath("/message/list");
        page.setLimit(10);
        List<Map<String, Object>> messageListVo = messageService.queryConversationsList(user, page);
        model.addAttribute("informUnreadCount",messageService.selectUnreadInformCount(user.getId(),null));
        model.addAttribute("letterUnreadCount",messageService.selectUnreadCount(user.getId(), null));
        model.addAttribute("messageList",messageListVo);
        return "/site/letter";
    }

    @GetMapping("/detail/{conversationId}/{target}")
    public String detail(@PathVariable("conversationId") String conversationId,@PathVariable("target") Integer target, Page page, Model model){
        page.setRows(messageService.selectLetterCount(conversationId));
        page.setPath("/message/detail/"+conversationId+"/"+target);
        page.setLimit(10);
        List<Message> messages = messageService.queryConversationDetail(conversationId, page);
        User user = userService.getUserById(target);
        model.addAttribute("messages",messages);
        model.addAttribute("target",user);
        return "/site/letter-detail";
    }

    @PostMapping("/send")
    @ResponseBody
    public String sendLetter(String targetName,String content){
        User target = userService.getUserByName(targetName);
        if (target==null){
            return CommunityUtil.getJSONString(1,"该用户不存在");
        }
        messageService.sendLetter(hostHolder.getUser(),target,content);
        return CommunityUtil.getJSONString(0,"发布成功!");
    }

    @PostMapping("/delete")
    @ResponseBody
    public String deleteLetter(Integer letterId){
        if (letterId==null){
            return CommunityUtil.getJSONString(1,"参数异常");
        }
        messageService.updateLetterStatus(Arrays.asList(letterId), 2);
        return CommunityUtil.getJSONString(0,"修改成功!");
    }

    @GetMapping("/inform")
    public String informList(Model model){
        User user = hostHolder.getUser();
        Map<String, Map<String, Object>> map = messageService.selectInformList(user.getId());
        model.addAttribute("informUnreadCount",messageService.selectUnreadInformCount(user.getId(),null));
        model.addAttribute("letterUnreadCount",messageService.selectUnreadCount(user.getId(),null));
        model.addAttribute("like",map.get("like"));
        model.addAttribute("follow",map.get("follow"));
        model.addAttribute("comment",map.get("comment"));
        model.addAttribute("inform",map.get("inform"));
        return "/site/notice";
    }

    @GetMapping("/informDetail/{topic}")
    public String informDetail(@PathVariable("topic")String topic,Page page,Model model){
        User user = hostHolder.getUser();
        page.setLimit(15);
        page.setPath("/message/informDetail/"+topic);
        page.setRows(messageService.selectTopicCount(user.getId(),topic));
        List<Map<String, Object>> result = messageService.selectTopicMessageList(user.getId(), topic, page);
        model.addAttribute("informList",result);
        return "/site/notice-detail";
    }

}
